package com.ruoyi.web.controller.h5.design;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.CaseService;
import com.ruoyi.system.service.IFavoriteService;
import com.ruoyi.system.vo.CaseDetailVo;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/h5/case")
public class CaseController {
    @Resource
    private CaseService caseService;

    @Resource
    private IFavoriteService favoriteService;

    /**
     * 案例编辑
     */
    @PostMapping("/edit")
    public AjaxResult editCase(@RequestBody CaseDetailVo vo){
        if(ObjectUtils.isEmpty(vo.getId())){
            return AjaxResult.error("主键不能为空");
        }
        return AjaxResult.success(caseService.editCase(vo));
    }
    /**
     * 案例详情（带收藏标识）
     */
    @PostMapping("/query")
    public AjaxResult queryCase(Long id){
        if(ObjectUtils.isEmpty(id)){
            return AjaxResult.error("主键不能为空");
        }
        CaseDetailVo caseDetail = caseService.queryCase(id.toString());
        
        // 如果用户已登录，添加收藏标识
        Long userId = SecurityUtils.getH5LoginUser().getUserId();
        if (userId != null && caseDetail != null) {
            boolean isFavorited = favoriteService.isFavorited(userId, "cases", caseDetail.getId());
            caseDetail.setIsFavorited(isFavorited);
        }
        
        return AjaxResult.success(caseDetail);
    }
    /**
     * 案例模糊搜索（带收藏标识）
     */
    @PostMapping("/query/condition")
    public AjaxResult querByCondition(@RequestBody CaseDetailVo vo){
        com.baomidou.mybatisplus.core.metadata.IPage<CaseDetailVo> pageResult = caseService.queryCaseByPage(vo);
        List<CaseDetailVo> caseList = pageResult.getRecords();
        
        // 如果用户已登录，添加收藏标识
        Long userId = SecurityUtils.getH5LoginUser().getUserId();
        if (userId != null && caseList != null && !caseList.isEmpty()) {
            // 提取案例ID列表
            List<Long> caseIds = caseList.stream()
                    .map(CaseDetailVo::getId)
                    .filter(Objects::nonNull)
                    .collect(java.util.stream.Collectors.toList());
            
            // 获取用户已收藏的案例ID列表
            List<Long> favoritedIds = favoriteService.getFavoritedBusinessIds(userId, "cases", caseIds);
            
            // 为每个案例添加收藏标识
            caseList.forEach(caseDetail -> {
                boolean isFavorited = favoritedIds.contains(caseDetail.getId());
                caseDetail.setIsFavorited(isFavorited);
            });
        }
        
        return AjaxResult.success(pageResult);
    }
    /**
     * 案例新增
     */
    @PostMapping("/add")
    public AjaxResult addCase(@RequestBody CaseDetailVo vo){
        return AjaxResult.success(caseService.addCase(vo));
    }
    /**
     * 案例逻辑删除
     */
    @PostMapping("delete/case")
    public AjaxResult deleteCaseByid(Long id){
        if(ObjectUtils.isEmpty(id)){
            return AjaxResult.error("主键不能为空");
        }
        return AjaxResult.success(caseService.deleteCaseById(id));
    }
    /**
     * 通过设计师id查询他的案例（带收藏标识）
     */
    @PostMapping("/query/case/by/designer")
    public AjaxResult queryCaseByDesignerId(Long id) {
        if (ObjectUtils.isEmpty(id)) {
            return AjaxResult.error("设计师id不能为空");
        }
        List<CaseDetailVo> caseDetailVoList = caseService.queryCaseByDesignerId(id);
        
        // 如果用户已登录，添加收藏标识
        Long userId = SecurityUtils.getH5LoginUser().getUserId();
        if (userId != null && !caseDetailVoList.isEmpty()) {
            // 提取案例ID列表
            List<Long> caseIds = caseDetailVoList.stream()
                    .map(CaseDetailVo::getId)
                    .filter(Objects::nonNull)
                    .collect(java.util.stream.Collectors.toList());
            
            // 获取用户已收藏的案例ID列表
            List<Long> favoritedIds = favoriteService.getFavoritedBusinessIds(userId, "cases", caseIds);
            
            // 为每个案例添加收藏标识
            caseDetailVoList.forEach(caseDetail -> {
                boolean isFavorited = favoritedIds.contains(caseDetail.getId());
                caseDetail.setIsFavorited(isFavorited);
            });
        }
        
        return AjaxResult.success(caseDetailVoList);
    }
}
